﻿/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows;
using Microsoft.Win32;
using System.IO;

namespace myBoard.Framework
{
    public class ExceptionsManager
    {
        private static string _configurationPath = Path.Combine(Environment.GetEnvironmentVariable("AppData"), "myBoard");
        private static string _logsPath = Path.Combine(_configurationPath, "Logs");

        // Methods
        public static void LogException(string source, Exception ex, bool show)
        {
            string details = ex.ToString();
            if (ex.InnerException != null)
            {
                details = details + "\r\n" + ex.InnerException.ToString();
            }

            try
            {                
                if (show)
                {
                    MBMessageBox box = new MBMessageBox(ex.ToString(), ex.Message, source, EventLogEntryType.Error, MessageBoxButton.OK);
                    box.ShowDialog();
                    box = null;
                }

                EventLog.WriteEntry(source, string.Format("{0}\n\n{1}", ex.Message, details), EventLogEntryType.Error);
            }
            catch
            {
                LogInFile(source, "Exception", ex.ToString(), EventLogEntryType.Error);                
            }
        }

        public static void LogException(string source, string title, Exception ex, EventLogEntryType type, bool show)
        {
            string details = ex.ToString();
            if (ex.InnerException != null)
                details = details + "\r\n" + ex.InnerException.ToString();
            if (show)
            {
                Forms.MessageBox.Show(title, ex.ToString(), source, type, MessageBoxButton.OK);
            }

            try
            {
                EventLog.WriteEntry(source, string.Format("{0}\n\n{1}", ex.Message, details), EventLogEntryType.Error);
            }
            catch
            {
                LogInFile(source, title, ex.ToString(), type);                
            }
        }

        public static void Write(string source, string title, string message, EventLogEntryType type, bool show)
        {
            if (show)
            {
                MBMessageBox box = new MBMessageBox(message, title, source, type, MessageBoxButton.OK);
                box.ShowDialog();
                box = null;
            }
            try
            {
                EventLog.WriteEntry(source, message, type);
            }
            catch
            {
                LogInFile(source, title, message, type);
            }
        }

        /// <summary>
        /// Use this method when we cannot have access to the events log service (UAC on ?)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="type"></param>
        public static void LogInFile(string source, string title, string message, EventLogEntryType type)
        {
            if (!Directory.Exists(_configurationPath))
                Directory.CreateDirectory(_configurationPath);
            if (!Directory.Exists(_logsPath))
                Directory.CreateDirectory(_logsPath);
            string logFile = Path.Combine(_logsPath, DateTime.Now.ToLongDateString()+".log");
            using (TextWriter tw = new StreamWriter(logFile, true))
            {
                tw.WriteLine(string.Format("Time : {0}", DateTime.Now.ToShortTimeString()));
                tw.WriteLine(string.Format("Source : {0}", source));
                tw.WriteLine(string.Format("Type : {0}", Enum.GetName(typeof(EventLogEntryType),type)));
                tw.WriteLine(string.Format("Title : {0}", title));
                tw.WriteLine(string.Format("Message : {0}", message));
                tw.WriteLine("####################################################################");
            }
        }
    }
}
